// ==========================================================================
// GFast自动生成logic操作代码。
// 生成日期：2024-11-11 20:52:35
// 生成路径: internal/app/system/logic/users.go
// 生成人：王中阳
// desc:用户
// company:云南奇讯科技有限公司
// ==========================================================================

package users

import (
	"context"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/tiger1103/gfast/v3/internal/app/system/consts"
	"github.com/tiger1103/gfast/v3/internal/app/system/dao"
	"github.com/tiger1103/gfast/v3/internal/app/system/model"
	"github.com/tiger1103/gfast/v3/internal/app/system/model/do"
	"github.com/tiger1103/gfast/v3/internal/app/system/service"
	"github.com/tiger1103/gfast/v3/library/liberr"
)

func init() {
	service.RegisterUsers(New())
}

func New() service.IUsers {
	return &sUsers{}
}

type sUsers struct{}

func (s *sUsers) List(ctx context.Context, req *model.UsersSearchReq) (listRes *model.UsersSearchRes, err error) {
	listRes = new(model.UsersSearchRes)
	err = g.Try(ctx, func(ctx context.Context) {
		m := dao.Users.Ctx(ctx).WithAll()
		if req.Id != "" {
			m = m.Where(dao.Users.Columns().Id+" = ?", req.Id)
		}
		if req.Username != "" {
			m = m.Where(dao.Users.Columns().Username+" like ?", "%"+req.Username+"%")
		}
		if req.Phone != "" {
			m = m.Where(dao.Users.Columns().Phone+" = ?", req.Phone)
		}
		if req.UserType != "" {
			m = m.Where(dao.Users.Columns().UserType+" = ?", gconv.Int(req.UserType))
		}
		if req.Openid != "" {
			m = m.Where(dao.Users.Columns().Openid+" = ?", req.Openid)
		}
		if req.Unionid != "" {
			m = m.Where(dao.Users.Columns().Unionid+" = ?", req.Unionid)
		}
		if req.Gender != "" {
			m = m.Where(dao.Users.Columns().Gender+" = ?", gconv.Int(req.Gender))
		}
		if req.Country != "" {
			m = m.Where(dao.Users.Columns().Country+" = ?", req.Country)
		}
		if req.Province != "" {
			m = m.Where(dao.Users.Columns().Province+" = ?", req.Province)
		}
		if req.City != "" {
			m = m.Where(dao.Users.Columns().City+" = ?", req.City)
		}
		if req.AvatarUrl != "" {
			m = m.Where(dao.Users.Columns().AvatarUrl+" = ?", req.AvatarUrl)
		}
		if req.IsBan != "" {
			m = m.Where(dao.Users.Columns().IsBan+" = ?", gconv.Int(req.IsBan))
		}
		if req.Score != "" {
			m = m.Where(dao.Users.Columns().Score+" = ?", gconv.Int(req.Score))
		}
		if req.SchoolId != "" {
			m = m.Where(dao.Users.Columns().SchoolId+" = ?", gconv.Int(req.SchoolId))
		}
		if req.IsFirst != "" {
			m = m.Where(dao.Users.Columns().IsFirst+" = ?", gconv.Int(req.IsFirst))
		}
		if req.PayQrCodePath != "" {
			m = m.Where(dao.Users.Columns().PayQrCodePath+" = ?", req.PayQrCodePath)
		}
		if req.Diamonds != "" {
			m = m.Where(dao.Users.Columns().Diamonds+" = ?", gconv.Int(req.Diamonds))
		}
		if req.TotalLotteryCount != "" {
			m = m.Where(dao.Users.Columns().TotalLotteryCount+" = ?", req.TotalLotteryCount)
		}
		if len(req.DateRange) != 0 {
			m = m.Where(dao.Users.Columns().CreatedAt+" >=? AND "+dao.Users.Columns().CreatedAt+" <=?", req.DateRange[0], req.DateRange[1])
		}
		listRes.Total, err = m.Count()
		liberr.ErrIsNil(ctx, err, "获取总行数失败")
		if req.PageNum == 0 {
			req.PageNum = 1
		}
		listRes.CurrentPage = req.PageNum
		if req.PageSize == 0 {
			req.PageSize = consts.PageSize
		}
		order := "id desc"
		if req.OrderBy != "" {
			order = req.OrderBy
		}
		var res []*model.UsersListRes
		err = m.Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
		liberr.ErrIsNil(ctx, err, "获取数据失败")
		listRes.List = make([]*model.UsersListRes, len(res))
		for k, v := range res {
			listRes.List[k] = &model.UsersListRes{
				Id:                v.Id,
				Username:          v.Username,
				Phone:             v.Phone,
				UserType:          v.UserType,
				Openid:            v.Openid,
				Unionid:           v.Unionid,
				Gender:            v.Gender,
				Country:           v.Country,
				Province:          v.Province,
				City:              v.City,
				AvatarUrl:         v.AvatarUrl,
				IsBan:             v.IsBan,
				Score:             v.Score,
				SchoolId:          v.SchoolId,
				LinkedSchoolId:    v.LinkedSchoolId,
				IsFirst:           v.IsFirst,
				PayQrCodePath:     v.PayQrCodePath,
				Wallet:            v.Wallet,
				Diamonds:          v.Diamonds,
				Balance:           v.Balance,
				TotalLotteryCount: v.TotalLotteryCount,
				CreatedAt:         v.CreatedAt,
			}
		}
	})
	return
}

func (s *sUsers) GetExportData(ctx context.Context, req *model.UsersSearchReq) (listRes []*model.UsersInfoRes, err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		m := dao.Users.Ctx(ctx).WithAll()
		if req.Id != "" {
			m = m.Where(dao.Users.Columns().Id+" = ?", req.Id)
		}
		if req.Username != "" {
			m = m.Where(dao.Users.Columns().Username+" like ?", "%"+req.Username+"%")
		}
		if req.Phone != "" {
			m = m.Where(dao.Users.Columns().Phone+" = ?", req.Phone)
		}
		if req.UserType != "" {
			m = m.Where(dao.Users.Columns().UserType+" = ?", gconv.Int(req.UserType))
		}
		if req.Openid != "" {
			m = m.Where(dao.Users.Columns().Openid+" = ?", req.Openid)
		}
		if req.Unionid != "" {
			m = m.Where(dao.Users.Columns().Unionid+" = ?", req.Unionid)
		}
		if req.Gender != "" {
			m = m.Where(dao.Users.Columns().Gender+" = ?", gconv.Int(req.Gender))
		}
		if req.Country != "" {
			m = m.Where(dao.Users.Columns().Country+" = ?", req.Country)
		}
		if req.Province != "" {
			m = m.Where(dao.Users.Columns().Province+" = ?", req.Province)
		}
		if req.City != "" {
			m = m.Where(dao.Users.Columns().City+" = ?", req.City)
		}
		if req.AvatarUrl != "" {
			m = m.Where(dao.Users.Columns().AvatarUrl+" = ?", req.AvatarUrl)
		}
		if req.IsBan != "" {
			m = m.Where(dao.Users.Columns().IsBan+" = ?", gconv.Int(req.IsBan))
		}
		if req.Score != "" {
			m = m.Where(dao.Users.Columns().Score+" = ?", gconv.Int(req.Score))
		}
		if req.SchoolId != "" {
			m = m.Where(dao.Users.Columns().SchoolId+" = ?", gconv.Int(req.SchoolId))
		}
		if req.IsFirst != "" {
			m = m.Where(dao.Users.Columns().IsFirst+" = ?", gconv.Int(req.IsFirst))
		}
		if req.PayQrCodePath != "" {
			m = m.Where(dao.Users.Columns().PayQrCodePath+" = ?", req.PayQrCodePath)
		}
		if req.Diamonds != "" {
			m = m.Where(dao.Users.Columns().Diamonds+" = ?", gconv.Int(req.Diamonds))
		}
		if req.TotalLotteryCount != "" {
			m = m.Where(dao.Users.Columns().TotalLotteryCount+" = ?", req.TotalLotteryCount)
		}
		if len(req.DateRange) != 0 {
			m = m.Where(dao.Users.Columns().CreatedAt+" >=? AND "+dao.Users.Columns().CreatedAt+" <=?", req.DateRange[0], req.DateRange[1])
		}
		if req.PageNum == 0 {
			req.PageNum = 1
		}
		if req.PageSize == 0 {
			req.PageSize = consts.PageSize
		}
		order := "id desc"
		if req.OrderBy != "" {
			order = req.OrderBy
		}
		err = m.Page(req.PageNum, req.PageSize).Order(order).Scan(&listRes)
		liberr.ErrIsNil(ctx, err, "获取数据失败")
	})
	return
}

// LinkedDataSearch 相关连表查询数据
func (s *sUsers) LinkedUsersDataSearch(ctx context.Context) (res *model.LinkedUsersDataSearchRes, err error) {
	res = new(model.LinkedUsersDataSearchRes)
	res.LinkedUsersSchools, err = s.ListUsersSchools(ctx)
	liberr.ErrIsNil(ctx, err, "获取关联表信息失败")
	return
}

func (s *sUsers) GetById(ctx context.Context, id int) (res *model.UsersInfoRes, err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		err = dao.Users.Ctx(ctx).WithAll().Where(dao.Users.Columns().Id, id).Scan(&res)
		liberr.ErrIsNil(ctx, err, "获取信息失败")
	})
	return
}

func (s *sUsers) Add(ctx context.Context, req *model.UsersAddReq) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.Users.Ctx(ctx).Insert(do.Users{
			Username:          req.Username,
			Phone:             req.Phone,
			UserType:          req.UserType,
			Openid:            req.Openid,
			Unionid:           req.Unionid,
			Gender:            req.Gender,
			Country:           req.Country,
			Province:          req.Province,
			City:              req.City,
			AvatarUrl:         req.AvatarUrl,
			IsBan:             req.IsBan,
			Score:             req.Score,
			SchoolId:          req.SchoolId,
			IsFirst:           req.IsFirst,
			PayQrCodePath:     req.PayQrCodePath,
			Wallet:            req.Wallet,
			Diamonds:          req.Diamonds,
			Balance:           req.Balance,
			TotalLotteryCount: req.TotalLotteryCount,
		})
		liberr.ErrIsNil(ctx, err, "添加失败")
	})
	return
}

func (s *sUsers) Edit(ctx context.Context, req *model.UsersEditReq) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.Users.Ctx(ctx).WherePri(req.Id).Update(do.Users{
			Username:          req.Username,
			Phone:             req.Phone,
			UserType:          req.UserType,
			Openid:            req.Openid,
			Unionid:           req.Unionid,
			Gender:            req.Gender,
			Country:           req.Country,
			Province:          req.Province,
			City:              req.City,
			AvatarUrl:         req.AvatarUrl,
			IsBan:             req.IsBan,
			Score:             req.Score,
			SchoolId:          req.SchoolId,
			IsFirst:           req.IsFirst,
			PayQrCodePath:     req.PayQrCodePath,
			Wallet:            req.Wallet,
			Diamonds:          req.Diamonds,
			Balance:           req.Balance,
			TotalLotteryCount: req.TotalLotteryCount,
		})
		liberr.ErrIsNil(ctx, err, "修改失败")
	})
	return
}

func (s *sUsers) Delete(ctx context.Context, ids []int) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.Users.Ctx(ctx).Delete(dao.Users.Columns().Id+" in (?)", ids)
		liberr.ErrIsNil(ctx, err, "删除失败")
	})
	return
}

func (s *sUsers) ListUsersSchools(ctx context.Context) (linkedUsersSchools []*model.LinkedUsersSchools, err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		err = dao.Schools.
			Ctx(ctx).
			Fields(model.LinkedUsersSchools{}).Scan(&linkedUsersSchools)
		liberr.ErrIsNil(ctx, err)
	})
	return
}
